/**************************************************   
名称: 图片轮播类   
创建时间: 2007-11-12   
示例:   
        页面中已经存在名为imgPlayer(或者别的ID也行)的节点.   
        PImgPlayer.addItem( "test", "http://kxgw8.cn", "http://kxgw8.cn/images/wy.jpg");   
        PImgPlayer.addItem( "test2", "http://kxgw8.cn", "http://kxgw8.cn/images/wy.jpg");   
        PImgPlayer.addItem( "test3", "http://kxgw8.cn", "http://kxgw8.cn/images/wy.jpg");   
        PImgPlayer.init( "imgPlayer", 200, 230 );   
备注:   
        适用于一个页面只有一个图片轮播的地方.   
***************************************************/   
var PImgPlayer = {   
        _timer : null,   
        _items : [],   
        _container : null,   
        _index : 0,   
        _imgs : [],   
        intervalTime : 3500,        //轮播间隔时间   
        init : function( objID, w, h, time ){   
                this.intervalTime = time || this.intervalTime;   
                this._container = document.getElementById( objID );   
                this._container.style.display = "block";   
                this._container.style.width = w + "px";   
                this._container.style.height = h + "px";   
                this._container.style.position = "relative";   
                this._container.style.overflow = "hidden";   
				
				//var flag=false;         
//				function DrawImage(ImgD){         
//					var image=new Image();         
//					var iwidth = w; 
//					//定义允许图片宽度         
//					var iheight = h; 
//					//定义允许图片高度         
//					image.src=ImgD.src;         
//					if(image.width>0 && image.height>0){         
//						flag=true;         
//						if(image.width/image.height>= iwidth/iheight){        
//							if(image.width>iwidth){         
//								ImgD.width=iwidth;         
//								ImgD.height=(image.height*iwidth)/image.width;         }
//							else{         
//								ImgD.width=image.width;         
//								ImgD.height=image.height;         }         
//						ImgD.alt=image.width+"×"+image.height;         }         
//						else{         
//							if(image.height>iheight){ 
//								ImgD.height=iheight;         
//								ImgD.width=(image.width*iheight)/image.height;         
//							}else{         
//								ImgD.width=image.width;         
//								ImgD.height=image.height;         }         
//							ImgD.alt=image.width+"×"+image.height;         
//							}         
//							}         
//							}
                
				//this._container.style.border = "1px solid #fff";   
                var linkStyle = "display: block; TEXT-DECORATION: none;";   
                if( document.all ){   
                        linkStyle += "FILTER:";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Barn(duration=0.5, motion='out', orientation='vertical') ";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Barn ( duration=0.5,motion='out',orientation='horizontal') ";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Blinds ( duration=0.5,bands=10,Direction='down' )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.CheckerBoard()";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Fade(duration=0.5,overlap=0)";   
                        linkStyle += "progid:DXImageTransform.Microsoft.GradientWipe ( duration=1,gradientSize=1.0,motion='reverse' )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Inset ()";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=PLUS,motion=out )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=PLUS,motion=in )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=DIAMOND,motion=in )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=SQUARE,motion=in )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Iris ( duration=0.5,irisStyle=STAR,motion=in )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.RadialWipe ( duration=0.5,wipeStyle=CLOCK )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.RadialWipe ( duration=0.5,wipeStyle=WEDGE )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.RandomBars ( duration=0.5,orientation=horizontal )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.RandomBars ( duration=0.5,orientation=vertical )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.RandomDissolve ()";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Spiral ( duration=0.5,gridSizeX=16,gridSizeY=16 )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Stretch ( duration=0.5,stretchStyle=PUSH )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Strips ( duration=0.5,motion=rightdown )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Wheel ( duration=0.5,spokes=8 )";   
                        linkStyle += "progid:DXImageTransform.Microsoft.Zigzag ( duration=0.5,gridSizeX=4,gridSizeY=40 ); width: 100%; height: 100%";   
                }   
                //   
                var ulStyle = "margin:0;width:"+w+"px;position:absolute;z-index:999;right:0;overflow: hidden;bottom:0";   
                //   
                var liStyle = "list-style-type: none; margin:0 2px 0 0;padding:0; float:right;top:10px";   
                //   
                var baseSpacStyle = "clear:both; display:block;height:25px;line-height:25px; font-size:14px;font-weight:bold;";   
                baseSpacStyle += "width:20px;height:25px;font-size:14px;font-weight:bold";   
                baseSpacStyle += "color:#000;text-align:center; cursor:pointer; ";   
                //   
                var ulHTML = "";   
                for(var i = this._items.length -1; i >= 0; i--){   
                        var spanStyle = "";   
                        if( i==this._index ){   
                                spanStyle = baseSpacStyle + "background:#3B81CD;height:17px;line-height:17px;width:17px;margin-right:2px;color:#000";   
                        } else {                                   
                                spanStyle = baseSpacStyle + "background:#fff;height:17px;line-height:17px;width:17px;margin-right:2px;color:#000;border:1px solid #3B81CD";   
                        }   
                        ulHTML += "<li style=\""+liStyle+"\">";   
                        ulHTML += "<span onmouseover=\"PImgPlayer.mouseOver(this);\" onmouseout=\"PImgPlayer.mouseOut(this);\" style=\""+spanStyle+"\" onclick=\"PImgPlayer.play("+i+");return false;\" herf=\"javascript:;\" title=\"" + this._items[i].title + "\">" + (i+1) + "</span>";   
                        ulHTML += "</li>";   
                }   
                //   
                var html = "<a  title=\""+this._items[this._index].title+"\" target=\"_blank\" style=\""+linkStyle+"\"></a><ul style=\""+ulStyle+"\">"+ulHTML+"</ul>";   
                this._container.innerHTML = html;   
                var link = this._container.getElementsByTagName("A")[0];           
                link.style.width = w + "px";   
                link.style.height = h + "px";
				var width = picW[0];
				var height = picH[0];
				link.innerHTML = "<img src='" + this._items[0].img + "' width='"+ width +"' height='"+ height +"'/>";
                //link.style.background = 'url(' + this._items[0].img + ') no-repeat center center';   
                //   
                this._timer = setInterval( "PImgPlayer.play()", this.intervalTime );   
        },   
        addItem : function( _title, _link, _imgURL ){   
                this._items.push ( {title:_title, link:_link, img:_imgURL } );   
                var img = new Image();   
                img.src = _imgURL;
				
					var flag = false;
					var iwidth=332;
					var iheight=188;
					var width = img.width;
					var height = img.height;
					if(img.width>0 && img.height>0){         
						flag=true;         
						if(width/height>= iwidth/iheight){ 
							if(width>iwidth){         
								img.width=iwidth;         
								img.height=(height*iwidth)/width;
								
							}else{         
								img.width=img.width;         
								img.height=img.height; 
								        
							}         
							//img.alt=img.width+"×"+img.height;         
						}else{         
							if(height>iheight){ 
								img.height=iheight;         
								img.width=(width*iheight)/height;  
								      
							}else{      
							
								  img.width=img.width;         
								img.height=img.height;
								     
							}         
							//img.alt=img.width+"×"+img.height;         
						}         
					}else if(height == 0){
							    	img.width=332;         
								img.height=188;
							    	
					}
				 
               
				this._imgs.push( img ); 
				picW[titleIndex] = img.width;
				picH[titleIndex] = img.height;
				picTitle[titleIndex] = _title;
				titleIndex++;
				
        },   
        play : function( index ){   
                if( index!=null ){   
                        this._index = index;   
                        clearInterval( this._timer );   
                        this._timer = setInterval( "PImgPlayer.play()", this.intervalTime );   
                } else {   
                        this._index = this._index<this._items.length-1 ? this._index+1 : 0;   
                }   
                var link = this._container.getElementsByTagName("a")[0];           
                if(link.filters){   
                        var ren = Math.floor(Math.random()*(link.filters.length));   
                        link.filters[ren].Apply();   
                        link.filters[ren].play();   
                }   
                
				
				var width = picW[this._index];
				var height = picH[this._index];
				link.innerHTML = "<img onclick=\"goDetailAction("+this._items[this._index].link+")\" src='" + this._items[this._index].img + "' width='"+ width +"' height='"+ height +"'/>";
				
			//	link.href = this._items[this._index].link;   
                link.title = this._items[this._index].title;   
                //link.style.background = 'url(' + this._items[this._index].img + ') no-repeat center center';
                //   
                var liStyle = "margin:0;list-style-type: none; margin:0;padding:0; float:right;";   
                var baseSpacStyle = "clear:both; display:block; width:20px;font-size:14px;font-weight:bold";   
                baseSpacStyle += "border-right:0;border-bottom:0;";   
                baseSpacStyle += "color:#000;text-align:center; cursor:pointer; ";   
                var ulHTML = "";   
                for(var i = this._items.length -1; i >= 0; i--){   
                        var spanStyle = "";   
                        if( i==this._index ){   
                                spanStyle = baseSpacStyle + "background:#3B81CD;height:17px;line-height:17px;width:17px;margin-right:2px;color:#000";   
                        } else {                                   
                                spanStyle = baseSpacStyle + "background:#fff;height:17px;line-height:17px;width:17px;margin-right:2px;color:#000;border:1px solid #3B81CD";   
                        }   
                        ulHTML += "<li style=\""+liStyle+"\">";   
                        ulHTML += "<span onmouseover=\"PImgPlayer.mouseOver(this);\" onmouseout=\"PImgPlayer.mouseOut(this);\" style=\""+spanStyle+"\" onclick=\"PImgPlayer.play("+i+");return false;\" herf=\"javascript:;\" title=\"" + this._items[i].title + "\">" + (i+1) + "</span>";   
                        ulHTML += "</li>";   
                }   
                this._container.getElementsByTagName("UL")[0].innerHTML = ulHTML;
				document.getElementById("showpictitle").innerHTML = picTitle[this._index];
        },   
        mouseOver : function(obj){   
                var i = parseInt( obj.innerHTML );   
                if( this._index!=i-1){   
                        obj.style.color = "#ff0000";   
                }   
        },   
        mouseOut : function(obj){   
                obj.style.color = "#000";   
        }   
}   